import 'dart:math';

import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_expandable_text/flutter_expandable_text.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:jinke/utils/fun_util.dart';
import 'package:photo_browser/photo_browser.dart';

import '../../../utils/colors_util.dart';
import '../../../utils/size_fit.dart';
import '../index.dart';
import '../models/ListWork.dart';

/// hello
class HelloWidget extends GetView<AttractController> {
  HelloWidget({Key? key}) : super(key: key);


  @override
  Widget build(BuildContext context) {

    // List<Content> contents = controller.state.listWork.content??[];

    return Column(
      children: [
        Container(
          color: Colors.white,
          child: BrnTabBar(
            controller: controller.state.tabCtrl,
            labelColor: ColorsUtil.blue,
            indicatorColor: ColorsUtil.blue,
            labelStyle: TextStyle(fontSize: 16.r,color: ColorsUtil.blue,fontWeight: FontWeight.bold),
            unselectedLabelStyle: TextStyle(fontSize: 16.r,color: ColorsUtil.subTitle,fontWeight: FontWeight.w500),
            tabs: [BadgeTab(text: '全部'),BadgeTab(text: '郑州')],
            onTap: (state, index) {
              state.refreshBadgeState(index);
            },
          ),),
        Expanded(child: Padding(
          padding: EdgeInsets.fromLTRB(16.r,25.r,16.r,25.r),
          child: TabBarView(
            controller: controller.state.tabCtrl,
            children: [
              ListView.builder(
                itemCount: controller.state.contents.length,
                itemBuilder: (context, index) {
                  return BrnStepLine(
                    lineWidth: 1.r,
                    //非第一个是灰色
                    isGrey: index !=0 ,
                    //最后一个的线条为透明色 做到不显示的效果
                    lineColor: Color(0xFFD9DADF),
                    highlightColor: ColorsUtil.blue,
                    normalColor: Color(0xFFD9DADF),
                    contentWidget: Container(
                      padding: EdgeInsets.only(left: 8),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Text(
                            controller.state.contents[index].startTime??'',
                            style: TextStyle(
                              color: index==0?ColorsUtil.blue:const Color(0xff333333),
                              fontSize: 14.r,
                              fontWeight: FontWeight.bold
                            ),
                          ),

                          ListView.builder(itemBuilder: (context,sonIndex){
                            return Container(
                              margin: EdgeInsets.only(bottom: 0.r),
                              child: ListView.builder(itemBuilder: (context,userIndex){
                                return Container(
                                  decoration: BoxDecoration(
                                      color: Colors.white,
                                      borderRadius: BorderRadius.circular(4.r)
                                  ),
                                  margin: EdgeInsets.only(top: 10.r,bottom: 5.r),
                                  padding: EdgeInsets.all(16.r),
                                  child: Column(
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    children: [
                                      Row(

                                        children: [
                                          Container(
                                            width: 26.r,
                                            height: 26.r,
                                            margin: EdgeInsets.only(right: 6.r),
                                            decoration: BoxDecoration(
                                                color:Color.fromRGBO(Random().nextInt(256), Random().nextInt(256), Random().nextInt(256), 1),
                                                borderRadius: BorderRadius.circular(180)
                                            ),

                                            child: Center(child: Text(controller.state.contents[index].userWorks![sonIndex].currentUserName.toString().substring(0,1),style: TextStyle(color: Colors.white,fontSize: 12.r,fontWeight: FontWeight.bold),),),
                                          ),
                                          Text(controller.state.contents[index].userWorks![sonIndex].currentUserName??'',style: TextStyle(color: const Color(0xff333333),fontWeight: FontWeight.w500,fontSize: 14.r),),
                                          SizedBox(width: 9.r,),
                                          Container(
                                            margin: EdgeInsets.only(top: 3.r),
                                            child: Text(controller.state.contents[index].userWorks![sonIndex].workContents![userIndex].createTime.toString().substring(controller.state.contents[index].userWorks![sonIndex].createTime!.length-5),style: TextStyle(color: const Color(0xff666666),fontWeight: FontWeight.w500,fontSize: 12.r),),
                                          ),

                                        ],
                                      ),
                                      SizedBox(height: 10.r,),
                                      ExpandableText(
                                        controller.state.contents[index].userWorks![sonIndex].workContents![userIndex].content.toString().trim(),

                                        style: TextStyle(color: Color(0xff333333),fontWeight: FontWeight.w500,fontSize: 16.r),
                                        readLessText: '收回',
                                        readMoreText: '展开',
                                        trim: 3,
                                      ),
                                      Visibility(visible: controller.state.contents[index].userWorks![sonIndex].workContents![userIndex].files!.isNotEmpty,child: GridView.builder(gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                                          crossAxisCount: 3,
                                          crossAxisSpacing: 10,
                                          mainAxisSpacing: 10,
                                          childAspectRatio: 1), itemBuilder: (context,filesindex){
                                        int ran = Random().nextInt(9999999);
                                        return GestureDetector(
                                          child: Hero(tag: ran.toString(),
                                            child: ClipRRect(
                                                borderRadius: BorderRadius.circular(4.r),
                                                child: Image.network(controller.state.contents[index].userWorks![sonIndex].workContents![userIndex].files![filesindex].sourcePath.toString(),fit: BoxFit.cover)),),
                                          onTap: (){
                                            PhotoBrowser photoBrowser = PhotoBrowser(
                                                itemCount: controller.state.contents[index].userWorks![sonIndex].workContents![userIndex].files!.length,
                                                initIndex: filesindex,
                                                controller: PhotoBrowerController(),
                                                allowTapToPopBuilder: (int index) {
                                                  if (index == 7) {
                                                    return false;
                                                  }
                                                  return true;
                                                },
                                                allowSwipeDownToPop: true,
                                                // If allowPullDownToPop is true, the allowSwipeDownToPop setting is invalid.
                                                // 如果allowPullDownToPop为true，则allowSwipeDownToPop设置无效
                                                allowPullDownToPop: true,
                                                heroTagBuilder: (int index) {
                                                  return ran.toString();
                                                },
                                                displayTypeBuilder: (int index) {
                                                  return DisplayType.image;
                                                },
                                                imageUrlBuilder: (int urlBuilderindex) {
                                                  return controller.state.contents[index].userWorks![sonIndex].workContents![userIndex].files![urlBuilderindex].sourcePath.toString();
                                                },
                                                // Thumbnails setting.
                                                // 缩略图设置
                                                thumImageUrlBuilder: (int thumUrlindex) {
                                                  return controller.state.contents[index].userWorks![sonIndex].workContents![userIndex].files![thumUrlindex].sourcePath.toString();
                                                }
                                            );

                                            photoBrowser.push(context);
                                          },
                                        );
                                      },itemCount: controller.state.contents[index].userWorks![sonIndex].workContents![userIndex].files?.length??0,shrinkWrap: true,physics: NeverScrollableScrollPhysics(),padding: EdgeInsets.only(top: 15.r),),),
                                      SizedBox(height: 12.r,),
                                      Row(
                                        children: [
                                          Icon(Icons.location_on_rounded,color: Color(0xff999999),size: 12,),
                                          Text(controller.state.contents[index].userWorks![sonIndex].areaName??'',style: TextStyle(color: Color(0xff999999),fontSize: 12.r,fontWeight: FontWeight.w500),)
                                        ],
                                      )
                                    ],
                                  ),
                                );
                              },itemCount: controller.state.contents[index].userWorks![sonIndex].workContents!.length,physics: NeverScrollableScrollPhysics(),shrinkWrap: true,),
                            );
                          },physics: const NeverScrollableScrollPhysics(),shrinkWrap: true,itemCount: controller.state.contents[index].userWorks!.length,),
                          SizedBox(height: 20.r,)
                        ],
                      ),
                    ),
                  );
                },
              ),
              ListView.builder(
                itemCount: controller.state.contents.length,
                itemBuilder: (context, index) {
                  return BrnStepLine(
                    lineWidth: 1.r,
                    //非第一个是灰色
                    isGrey: index !=0 ,
                    //最后一个的线条为透明色 做到不显示的效果
                    lineColor: Color(0xFFD9DADF),
                    highlightColor: ColorsUtil.blue,
                    normalColor: Color(0xFFD9DADF),
                    contentWidget: Container(
                      padding: EdgeInsets.only(left: 8),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Text(
                            controller.state.contents[index].startTime??'',
                            style: TextStyle(
                                color: index==0?ColorsUtil.blue:const Color(0xff333333),
                                fontSize: 14.r,
                                fontWeight: FontWeight.bold
                            ),
                          ),

                          ListView.builder(itemBuilder: (context,sonIndex){
                            return Container(
                              margin: EdgeInsets.only(bottom: 0.r),
                              child: ListView.builder(itemBuilder: (context,userIndex){
                                return Container(
                                  decoration: BoxDecoration(
                                      color: Colors.white,
                                      borderRadius: BorderRadius.circular(4.r)
                                  ),
                                  margin: EdgeInsets.only(top: 10.r,bottom: 5.r),
                                  padding: EdgeInsets.all(16.r),
                                  child: Column(
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    children: [
                                      Row(

                                        children: [
                                          Container(
                                            width: 26.r,
                                            height: 26.r,
                                            margin: EdgeInsets.only(right: 6.r),
                                            decoration: BoxDecoration(
                                                color:Color.fromRGBO(Random().nextInt(256), Random().nextInt(256), Random().nextInt(256), 1),
                                                borderRadius: BorderRadius.circular(180)
                                            ),

                                            child: Center(child: Text(controller.state.contents[index].userWorks![sonIndex].currentUserName.toString().substring(0,1),style: TextStyle(color: Colors.white,fontSize: 12.r,fontWeight: FontWeight.bold),),),
                                          ),
                                          Text(controller.state.contents[index].userWorks![sonIndex].currentUserName??'',style: TextStyle(color: const Color(0xff333333),fontWeight: FontWeight.w500,fontSize: 14.r),),
                                          SizedBox(width: 9.r,),
                                          Container(
                                            margin: EdgeInsets.only(top: 3.r),
                                            child: Text(controller.state.contents[index].userWorks![sonIndex].workContents![userIndex].createTime.toString().substring(controller.state.contents[index].userWorks![sonIndex].createTime!.length-5),style: TextStyle(color: const Color(0xff666666),fontWeight: FontWeight.w500,fontSize: 12.r),),
                                          ),

                                        ],
                                      ),
                                      SizedBox(height: 10.r,),
                                      BrnExpandableText(
                                        text:controller.state.contents[index].userWorks![sonIndex].workContents![userIndex].content.toString().trim(),
                                        textStyle: TextStyle(color: Color(0xff333333),fontWeight: FontWeight.w500,fontSize: 16.r),
                                        maxLines: 3,
                                      ),
                                      Visibility(visible: controller.state.contents[index].userWorks![sonIndex].workContents![userIndex].files!.isNotEmpty,child: GridView.builder(gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                                          crossAxisCount: 3,
                                          crossAxisSpacing: 10,
                                          mainAxisSpacing: 10,
                                          childAspectRatio: 1), itemBuilder: (context,filesindex){
                                        int ran = Random().nextInt(9999999);
                                        return GestureDetector(
                                          child: Hero(tag: ran.toString(),
                                            child: ClipRRect(
                                                borderRadius: BorderRadius.circular(4.r),
                                                child: Image.network(controller.state.contents[index].userWorks![sonIndex].workContents![userIndex].files![filesindex].sourcePath.toString(),fit: BoxFit.cover)),),
                                          onTap: (){
                                            PhotoBrowser photoBrowser = PhotoBrowser(
                                                itemCount: controller.state.contents[index].userWorks![sonIndex].workContents![userIndex].files!.length,
                                                initIndex: filesindex,
                                                controller: PhotoBrowerController(),
                                                allowTapToPopBuilder: (int index) {
                                                  if (index == 7) {
                                                    return false;
                                                  }
                                                  return true;
                                                },
                                                allowSwipeDownToPop: true,
                                                // If allowPullDownToPop is true, the allowSwipeDownToPop setting is invalid.
                                                // 如果allowPullDownToPop为true，则allowSwipeDownToPop设置无效
                                                allowPullDownToPop: true,
                                                heroTagBuilder: (int index) {
                                                  return ran.toString();
                                                },
                                                displayTypeBuilder: (int index) {
                                                  return DisplayType.image;
                                                },
                                                imageUrlBuilder: (int urlBuilderindex) {
                                                  return controller.state.contents[index].userWorks![sonIndex].workContents![userIndex].files![urlBuilderindex].sourcePath.toString();
                                                },
                                                // Thumbnails setting.
                                                // 缩略图设置
                                                thumImageUrlBuilder: (int thumUrlindex) {
                                                  return controller.state.contents[index].userWorks![sonIndex].workContents![userIndex].files![thumUrlindex].sourcePath.toString();
                                                }
                                            );

                                            photoBrowser.push(context);
                                          },
                                        );
                                      },itemCount: controller.state.contents[index].userWorks![sonIndex].workContents![userIndex].files?.length??0,shrinkWrap: true,physics: NeverScrollableScrollPhysics(),padding: EdgeInsets.only(top: 15.r),),),
                                      SizedBox(height: 12.r,),
                                      Row(
                                        children: [
                                          Icon(Icons.location_on_rounded,color: Color(0xff999999),size: 12,),
                                          Text(controller.state.contents[index].userWorks![sonIndex].areaName??'',style: TextStyle(color: Color(0xff999999),fontSize: 12.r,fontWeight: FontWeight.w500),)
                                        ],
                                      )
                                    ],
                                  ),
                                );
                              },itemCount: controller.state.contents[index].userWorks![sonIndex].workContents!.length,physics: NeverScrollableScrollPhysics(),shrinkWrap: true,),
                            );
                          },physics: const NeverScrollableScrollPhysics(),shrinkWrap: true,itemCount: controller.state.contents[index].userWorks!.length,),
                          SizedBox(height: 20.r,)
                        ],
                      ),
                    ),
                  );
                },
              ),
            ],
          ),
        ))
      ],
    );
  }
}
